/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package schedule.listener;

import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.listeners.SchedulerListenerSupport;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import lombok.extern.log4j.Log4j;

/**
 * @author Mht
 * @date Jun 2, 2016
 */
@Log4j
@Component("scheduleJobListener")
public class ScheduleJobListener extends SchedulerListenerSupport {

	@Override
	public void schedulerStarted() {
		// do something with the event
		log.info("Scheduler was starting");
	}

	@Override
	public void schedulerShutdown() {
		// do something with the event
		log.info("Scheduler was shuttingdown");
	}

	@Override
	public void jobAdded(JobDetail jobDetail) {
		Assert.notNull(jobDetail, "JobDetail should not be null!");
		JobKey jobKey = jobDetail.getKey();
		String jobName = jobKey.getName();
		String jobGroup = jobKey.getGroup();
		log.info(String.format("[%s] job added to job group [%s]", new Object[] { jobName, jobGroup }));
	}

	@Override
	public void jobDeleted(JobKey jobKey) {
		Assert.notNull(jobKey, "JobKey should not be null!");
		String jobName = jobKey.getName();
		String jobGroup = jobKey.getGroup();
		log.info(String.format("[%s] job delete from job group [%s]", new Object[] { jobName, jobGroup }));
	}

	@Override
	public void jobPaused(JobKey jobKey) {
		Assert.notNull(jobKey, "JobKey should not be null!");
		String jobName = jobKey.getName();
		String jobGroup = jobKey.getGroup();
		log.info(String.format("[%s] job paused in group [%s]", new Object[] { jobName, jobGroup }));
	}

	@Override
	public void jobResumed(JobKey jobKey) {
		Assert.notNull(jobKey, "JobKey should not be null!");
		String jobName = jobKey.getName();
		String jobGroup = jobKey.getGroup();
		log.info(String.format("[%s] job resumed in group [%s]", new Object[] { jobName, jobGroup }));
	}

	public void jobScheduled(Trigger trigger) {
		Assert.notNull(trigger, "trigger should not be null!");
		JobKey jobKey = trigger.getJobKey();

		Assert.notNull(trigger, "JobKey should not be null!");
		String jobName = jobKey.getName();
		String jobGroup = jobKey.getGroup();
		log.info(String.format("[%s] job added to job group [%s]", new Object[] { jobName, jobGroup }));
	}

	public void jobsPaused(String jobGroup) {
		log.info(String.format("pause all job in group [%s]", jobGroup));
	}

	public void jobsResumed(String jobGroup) {
		log.info(String.format("resumed all job in group [%s]", jobGroup));
	}

	public void jobUnscheduled(TriggerKey triggerKey) {
		String triggerName = triggerKey.getName();
		String triggerGroup = triggerKey.getGroup();
		log.info(
				String.format("unschedule all trigger [%s] in group [%s]", new Object[] { triggerName, triggerGroup }));
	}

	public void schedulerError(String msg, SchedulerException cause) {
		cause.printStackTrace();
		log.info(String.format("Scheduler excception [%s]", msg));
	}

	public void triggerFinalized(Trigger trigger) {
	}

	public void triggerPaused(TriggerKey triggerKey) {
	}

	public void triggerResumed(TriggerKey triggerKey) {
	}

	public void triggersPaused(String triggerGroup) {
	}

	public void triggersResumed(String triggerGroup) {
	}

	public void schedulingDataCleared() {
	}
}
